﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using System.Xml;

namespace YArchitech.Controls.HeaderGridView
{
	public class HeaderUtils
	{
		public static void CopyTree(TreeNodeCollection tc, TreeView tvTo)
		{
			tvTo.Nodes.Clear();
			TreeNode[] array = new TreeNode[tc.Count];
			tc.CopyTo(array, 0);
			for (int i = 0; i < array.Count<TreeNode>(); i++)
			{
				tvTo.Nodes.Add((TreeNode)array[i].Clone());
			}
		}

		public static DataTable ToDataTable<T>(IEnumerable<T> list, List<TreeNode> pList, Dictionary<string, string> dict)
		{
			Type typeFromHandle = typeof(T);
			DataTable dataTable = new DataTable();
			foreach (TreeNode treeNode in pList)
			{
				if (treeNode != null)
				{
					dataTable.Columns.Add(treeNode.Text);
				}
			}
			foreach (T t in list)
			{
				DataRow dataRow = dataTable.NewRow();
				foreach (TreeNode treeNode2 in pList)
				{
					if (treeNode2 != null && treeNode2.Tag != null && treeNode2.Tag.ToString() != "")
					{
						PropertyInfo[] properties = typeFromHandle.GetProperties();
						for (int i = 0; i < properties.Count<PropertyInfo>(); i++)
						{
							if (treeNode2.Tag.ToString() != "hide" && dict[treeNode2.Tag.ToString()] == properties[i].Name)
							{
								dataRow[treeNode2.Text] = properties[i].GetValue(t, null);
							}
						}
					}
				}
				dataTable.Rows.Add(dataRow);
			}
			return dataTable;
		}

		public static List<TreeNode> GetChilds(TreeNodeCollection tnc)
		{
			List<TreeNode> list = new List<TreeNode>();
			if (tnc.Count > 0)
			{
				foreach (object obj in tnc)
				{
					TreeNode treeNode = (TreeNode)obj;
					if (treeNode.Nodes.Count == 0)
					{
						list.Add(treeNode);
					}
					else
					{
						list.AddRange(HeaderUtils.GetChilds(treeNode.Nodes));
					}
				}
			}
			return list;
		}

		private static void get_ChildNodes(XmlNode ParentNode, TreeNode tvNode)
		{
			foreach (object obj in ParentNode.ChildNodes)
			{
				XmlNode xmlNode = (XmlNode)obj;
				TreeNode treeNode = tvNode.Nodes.Add(xmlNode.Name);
				if (xmlNode.Attributes["对应属性"] != null)
				{
					treeNode.Tag = xmlNode.Attributes["对应属性"].Value;
				}
				if (xmlNode.ChildNodes != null)
				{
					HeaderUtils.get_ChildNodes(xmlNode, treeNode);
				}
			}
		}

		public static void LoadXml(string fileName, TreeView tv)
		{
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(fileName);
			foreach (object obj in xmlDocument.ChildNodes[1].ChildNodes)
			{
				XmlNode xmlNode = (XmlNode)obj;
				TreeNode treeNode = tv.Nodes.Add(xmlNode.Name);
				if (xmlNode.Attributes["对应属性"] != null)
				{
					treeNode.Tag = xmlNode.Attributes["对应属性"].Value;
				}
				if (xmlNode.ChildNodes != null)
				{
					HeaderUtils.get_ChildNodes(xmlNode, treeNode);
				}
			}
		}

		public static void CreateHeaderXml(string fileName)
		{
			XmlDocument xmlDocument = new XmlDocument();
			XmlNode newChild = xmlDocument.CreateXmlDeclaration("1.0", "gb2312", null);
			xmlDocument.AppendChild(newChild);
			XmlElement newChild2 = xmlDocument.CreateElement("", "Header", "");
			xmlDocument.AppendChild(newChild2);
			xmlDocument.Save(fileName);
		}

		public static void WriteToXml(TreeView tv, string fileName)
		{
			if (tv.Nodes.Count > 0)
			{
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.Load(fileName);
				XmlNode xmlNode = xmlDocument.SelectSingleNode("Header");
				foreach (object obj in tv.Nodes)
				{
					TreeNode treeNode = (TreeNode)obj;
					XmlElement xmlElement = xmlDocument.CreateElement(treeNode.Text);
					if (treeNode.Tag != null)
					{
						xmlElement.SetAttribute("对应属性", treeNode.Tag.ToString());
					}
					xmlNode.AppendChild(xmlElement);
					if (treeNode.Nodes != null)
					{
						HeaderUtils.set_ChildNods(treeNode, xmlElement);
					}
				}
				xmlDocument.Save(fileName);
			}
		}

		private static void set_ChildNods(TreeNode ParentNode, XmlElement element)
		{
			foreach (object obj in ParentNode.Nodes)
			{
				TreeNode treeNode = (TreeNode)obj;
				XmlElement xmlElement = element.OwnerDocument.CreateElement(treeNode.Text);
				if (treeNode.Tag != null)
				{
					xmlElement.SetAttribute("对应属性", treeNode.Tag.ToString());
				}
				element.AppendChild(xmlElement);
				if (treeNode.Nodes != null)
				{
					HeaderUtils.set_ChildNods(treeNode, xmlElement);
				}
			}
		}
	}
}
